import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


public class URL {

	public URL() {}
	
	public static void cache(String query, String DB, ArrayList<String> URLS, int num_results) {
		//check if URL cache directory exists, make it otherwise
		String dir_url = "cache/urls/" + DB + "/";
		String dir_matches = "cache/matches/" + DB + "/";

		if (!(new File(dir_url)).exists()){
		    (new File(dir_url)).mkdirs();
		}

		if (!(new File(dir_matches)).exists()){
		    (new File(dir_matches)).mkdirs();
		}
		
		//./cache/urls/<site>/query_file that contains each URL
		FileWriter fstream;
		try {
			fstream = new FileWriter(dir_url + query);
			BufferedWriter out = new BufferedWriter(fstream);
			
			for (String URL: URLS) {
				out.write(URL);
				out.newLine();
			}

			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


        try {
            fstream = new FileWriter(dir_matches + query);
            BufferedWriter out = new BufferedWriter(fstream);

			out.write(Integer.toString(num_results));

			out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}

	public static boolean hasCache(String DB, String query) {
        String dir_url = "cache/urls/" + DB + "/" + query;
        String dir_matches = "cache/matches/" + DB + "/" + query;

		if ((new File(dir_url)).exists() && ((new File(dir_matches)).exists() )) {
			return true;
		}
		return false;
	}



}
